package com.wanfajie.smsdistribute.main

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import com.wanfajie.smsdistribute.R
import com.wanfajie.smsdistribute.common.activity.BaseActivity
import com.wanfajie.smsdistribute.common.listener.NavigationViewPagerAdapter
import com.wanfajie.smsdistribute.main.fragment.group.PhoneGroupFragment
import com.wanfajie.smsdistribute.main.fragment.record.SendRecordFragment
import com.wanfajie.smsdistribute.main.fragment.template.TemplateManageFragment
import com.wanfajie.smsdistribute.ui.send_sms_bulk.SendSmsBulkActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity() {

    companion object {

        @JvmStatic
        private val fragments = mapOf(
                R.id.nav_template to TemplateManageFragment(),
                R.id.nav_send_record to SendRecordFragment(),
                R.id.nav_group to PhoneGroupFragment())

        @JvmStatic
        private val TAG = "MainActivity"

        @JvmStatic
        private val DEFAULT_MENU_ID = R.menu.menu_main
    }

    override val requiredPermissions: Array<String>? = arrayOf(Manifest.permission.READ_PHONE_STATE)

    private lateinit var navPagerAdapter : NavigationViewPagerAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        navPagerAdapter = NavigationViewPagerAdapter(supportFragmentManager, main_viewpager, navigation_bottom, fragments)
        navPagerAdapter.setOnChangedListener { i, _, fragment ->

            // XXX: 模板管理页FloatingActionButton可能遮挡模板控制按钮, 不得已而为之
            if (fragment is TemplateManageFragment) {
                fab.hide()
            } else {
                fab.show()
            }

            finishActionMode()
        }

        fab.setOnClickListener {
            val intent = Intent(this, SendSmsBulkActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        super.onCreateOptionsMenu(menu)
        menuInflater.inflate(DEFAULT_MENU_ID, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }
}
